package com.slice.upload.strategy;

import com.slice.upload.base.Strategy;
import com.slice.upload.strategy.biz.MappedByteBufferUploadStrategy;
import com.slice.upload.strategy.biz.RandomAccessUploadStrategy;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * @ClassName FileUploadHandler
 * @Description 上传文件策略处理器
 * @Author frinder
 * @Date 2021/1/27 17:49
 * @Verion 1.0
 */
@Slf4j
public class FileUploadHandler {

    @Resource
    private RandomAccessUploadStrategy randomAccessUploadStrategy;

    @Resource
    private MappedByteBufferUploadStrategy mappedByteBufferUploadStrategy;

    private BaseSliceUploadStrategy baseStrategy;

    @PostConstruct
    public void post() {
        baseStrategy = new BaseSliceUploadStrategy();
        baseStrategy
                .register(randomAccessUploadStrategy)
                .register(mappedByteBufferUploadStrategy);
    }

    @Strategy
    public void handle(FileUploadPayload payload) {
        baseStrategy.handle(payload, baseStrategy);
    }

}
